import config from './config'
const http = (options) => {
	return new Promise((resolve, reject) => {
		uni.request({
			header: {
				'Content-Type': 'application/json; charset=utf-8',
				'openid': uni.getStorageSync('openid') ? uni.getStorageSync(
					'openid') : ''
			},
			url: config.baseUrl + options.url,
			method: options.method || 'GET',
			data: options.data || {},
			timeout: 20000,
			success: (res) => {
				if (res.data.code != 0 && res.data.code) {
					if (res.data.code == 1001) {
						uni.showToast({
							title: res.data.msg,
							icon: "none"
						})
					} else if (res.data.code == 1002) {
						uni.showToast({
							title: res.data.msg,
							icon: "none"
						})
						setTimeout(() => {
							let path = 'pages/login/index'
							uni.$u.route({
								url: path,
							})
						}, 1000)
					}else if(res.data.code == -1){
						uni.showToast({
							title: res.data.msg,
							icon: "none"
						})
					}else {
						uni.showToast({
							title: '内部错误',
							icon: "none"
						})
					}
				}
				resolve(res.data)
			},
			fail: (err) => {
				uni.showToast({
					title: '网络连接失败，请打开网络!',
					icon: "none"
				})
				reject(err)
			}
		})
	})
}
export default http
